AC_PREREQ(2.54)
AC_INIT([galaxium], 0.7.0, [https://galaxium.bountysource.com])
AM_INIT_AUTOMAKE([foreign])

dnl AC_PROG_INTLTOOL([0.25])

AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
if test "x$PKG_CONFIG" = "xno"; then
        AC_MSG_ERROR([You need to install pkg-config])
fi

AC_PROG_INSTALL

AC_PATH_PROG(GCC, gcc, no)
if test "x$GCC" = "xno"; then
        AC_MSG_ERROR([gcc Not found])
fi

AC_PATH_PROG(GMCS, gmcs, no)
if test "x$GMCS" = "xno"; then
        AC_MSG_ERROR([gmcs Not found])
fi

MONO_REQUIRED_VERSION=1.2.4
PKG_CHECK_MODULES(MONO,mono >= $MONO_REQUIRED_VERSION, has_mono=true, has_mono=false)

if test "x$has_mono" = "xfalse"; then
      AC_MSG_ERROR([Please install mono version $MONO_REQUIRED_VERSION or later to install Galaxium.
Please see http://www.mono-project.org/ to download latest mono sources or packages])
fi

AC_SUBST(MONO)

GLIBSHARP_REQUIRED_VERSION=2.10.2
GTKSHARP_REQUIRED_VERSION=2.10.2
GLADESHARP_REQUIRED_VERSION=2.10.0
GECKOSHARP_REQUIRED_VERSION=0.10
NDESK_DBUS_REQUIRED_VERSION=0.4.2
NDESK_DBUS_GLIB_REQUIRED_VERSION=0.3
LIBANCULUS_REQUIRED_VERSION=0.3
MONO_ADDINS_VERSION=0.3
GSTREAMER_VERSION=0.10
WEBKIT_SHARP_REQUIRED_VERSION=0.2

PKG_CHECK_MODULES(GLIB_SHARP, glib-sharp-2.0 >= $GLIBSHARP_REQUIRED_VERSION)
AC_SUBST(GLIB_SHARP_LIBS)
PKG_CHECK_MODULES(GTK_SHARP, gtk-sharp-2.0 >= $GTKSHARP_REQUIRED_VERSION)
AC_SUBST(GTK_SHARP_LIBS)
PKG_CHECK_MODULES(GLADE_SHARP, glade-sharp-2.0 >= $GLADESHARP_REQUIRED_VERSION)
AC_SUBST(GLADE_SHARP_LIBS)
PKG_CHECK_MODULES(GSTREAMER, gstreamer-0.10 >= $GSTREAMER_VERSION)
AC_SUBST(GSTREAMER_LIBS)
PKG_CHECK_MODULES(NDESK_DBUS, ndesk-dbus-1.0 >= $NDESK_DBUS_REQUIRED_VERSION
	ndesk-dbus-glib-1.0 >= $NDESK_DBUS_GLIB_REQUIRED_VERSION)
AC_SUBST(NDESK_DBUS_LIBS)
PKG_CHECK_MODULES(MONO_ADDINS, mono-addins >= $MONO_ADDINS_VERSION
	mono-addins-setup >= $MONO_ADDINS_VERSION)
AC_SUBST(MONO_ADDINS_LIBS)
PKG_CHECK_MODULES(LIBANCULUS, anculus-core >= $LIBANCULUS_REQUIRED_VERSION
	anculus-core-extended >= $LIBANCULUS_REQUIRED_VERSION
	anculus-gui >= $LIBANCULUS_REQUIRED_VERSION)
AC_SUBST(LIBANCULUS_LIBS)

AC_SUBST(prefix)
AC_SUBST(srcdir)

gtksharp_prefix="`$PKG_CONFIG --variable=prefix gtk-sharp-2.0`"
AC_SUBST(gtksharp_prefix)

AC_ARG_ENABLE(msn, AC_HELP_STRING([--enable-msn], [enable MSN protocol support [default=yes]]), enable_msn=${enableval}, enable_msn=yes)

AC_ARG_ENABLE(adium, AC_HELP_STRING([--enable-adium], [enable AdiumX theme support [default=yes]]), enable_adium=${enableval}, enable_adium=yes)

AC_ARG_ENABLE(gecko, AC_HELP_STRING([--enable-gecko], [enable Gecko support [default=no]]), enable_gecko=${enableval}, enable_gecko=no)
AC_ARG_ENABLE(webkit, AC_HELP_STRING([--enable-webkit], [enable WebKit support [default=yes]]), enable_webkit=${enableval}, enable_webkit=yes)

AM_CONDITIONAL(ENABLE_PROTOCOL_MSN, [test x$enable_msn = xyes])

AM_CONDITIONAL(ENABLE_ADIUM, [test x$enable_adium = xyes])

AM_CONDITIONAL(ENABLE_GECKO, [test x$enable_gecko = xyes])
AM_CONDITIONAL(ENABLE_WEBKIT, [test x$enable_webkit = xyes])

if test "x$enable_gecko" = "xyes"; then
	PKG_CHECK_MODULES(GECKO_SHARP, gecko-sharp-2.0 >= $GECKOSHARP_REQUIRED_VERSION)
	AC_SUBST(GECKO_SHARP_LIBS)

	#We need to know where Mozilla is for the Html conversation widget
	#Check pkg-config first
	MOZILLA_HOME="`$PKG_CONFIG --variable=libdir mozilla-gtkmozembed`"
	if test -z "$MOZILLA_HOME"; then
		MOZILLA_HOME="`$PKG_CONFIG --variable=libdir firefox-gtkmozembed`"
	fi
	if test -z "$MOZILLA_HOME"; then
		MOZILLA_HOME="`$PKG_CONFIG --variable=libdir xulrunner-gtkmozembed`"
	fi

	#if MOZILLA_HOME is still empty, try to detect without pkg_config
	MOZFILE="libgtkembedmoz.so"
	find_libgtkembedmoz ()
	{
		MOZDIR=$MOZILLA_FIVE_HOME
		if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
	
		MOZDIR=$(grep -h GRE_PATH= /etc/gre.d/*.conf 2>/dev/null | cut -d '"' -f 2 -d = | head -n 1) #"
		if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
	
		mozilla_script=$(which mozilla 2> /dev/null)
		firefox_script=$(which firefox 2> /dev/null)
	
		if [ test -z $mozilla_script ] && [ test -z $firefox_script ]; then return; fi
		for runtime_script in "$firefox_script $mozilla_script"; do
			MOZDIR=$(grep "MOZ_DIST_LIB=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
			if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
			MOZDIR=$(grep "MOZILLA_FIVE_HOME=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
			if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
			MOZDIR=$(grep "MOZILLA_LIBDIR=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
			if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
			MOZDIR=$(grep "moz_libdir=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
			if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
		done
	}

	if test -e "$MOZILLA_HOME/$MOZFILE"; then
		echo "MOZILLA_HOME=$MOZILLA_HOME"
	else
		MOZILLA_HOME=`find_libgtkembedmoz`
		if test ! -e "$MOZILLA_HOME/$MOZFILE"; then
			MOZILLA_HOME=
			AC_MSG_WARN([Cannot detect Mozilla library directory. Please set MOZILLA_FIVE_HOME to your Mozilla directory.])
		fi
	fi
	
	AC_SUBST(MOZILLA_HOME)
fi

if test "x$enable_webkit" = "xyes"; then
	PKG_CHECK_MODULES(WEBKIT_SHARP, webkit-sharp-1.0 >= $WEBKIT_SHARP_REQUIRED_VERSION)
	AC_SUBST(WEBKIT_SHARP_LIBS)
fi

CSC_FLAGS="-debug -codepage:utf8"

GALAXIUM_DIR='$(prefix)/lib/galaxium'
GALAXIUM_DATA_DIR='$(datadir)/galaxium'

AC_SUBST(GALAXIUM_DATA_DIR)
AC_SUBST(GALAXIUM_DIR)

ASSEMBLY_VERSION=0.7.0.0
AC_SUBST(ASSEMBLY_VERSION)

AC_OUTPUT([
Makefile
dlls/Makefile
docs/Makefile
docs/man/Makefile
src/Makefile
src/Libraries/Makefile
src/Libraries/libgalaxium/Makefile
src/Libraries/libmimic-sharp/Makefile
src/Libraries/libswfdec-sharp/Makefile
src/Galaxium.AdiumThemes/Makefile
src/Galaxium.Client/Makefile
src/Galaxium.Client.Gtk/Makefile
src/Galaxium.Core/Makefile
src/Galaxium.Gecko/Makefile
src/Galaxium.GStreamer/Makefile
src/Galaxium.Gui/Makefile
src/Galaxium.Gui.Gtk/Makefile
src/Galaxium.Protocol/Makefile
src/Galaxium.Protocol.Gui/Makefile
src/Galaxium.Protocol.Msn/Makefile
src/Galaxium.Protocol.Msn.Gtk/Makefile
src/Galaxium.Startup/Makefile
src/Galaxium.WebKit/Makefile
src/Libraries/libmimic-sharp/AssemblyInfo.cs
src/Libraries/libswfdec-sharp/AssemblyInfo.cs
src/Galaxium.AdiumThemes/AssemblyInfo.cs
src/Galaxium.Client/AssemblyInfo.cs
src/Galaxium.Client.Gtk/AssemblyInfo.cs
src/Galaxium.Core/AssemblyInfo.cs
src/Galaxium.Gecko/AssemblyInfo.cs
src/Galaxium.GStreamer/AssemblyInfo.cs
src/Galaxium.Gui/AssemblyInfo.cs
src/Galaxium.Gui.Gtk/AssemblyInfo.cs
src/Galaxium.Protocol/AssemblyInfo.cs
src/Galaxium.Protocol.Gui/AssemblyInfo.cs
src/Galaxium.Protocol.Msn/AssemblyInfo.cs
src/Galaxium.Protocol.Msn.Gtk/AssemblyInfo.cs
src/Galaxium.Startup/AssemblyInfo.cs
src/Galaxium.Startup/Galaxium.Startup.exe.config
src/Galaxium.WebKit/AssemblyInfo.cs
build/Makefile
build/Data/Makefile
build/Data/Themes/Makefile
build/Data/Themes/AdiumEmoticons/Makefile
build/Data/Themes/AdiumMessageStyles/Makefile
build/Data/Themes/AdiumSoundSets/Makefile
build/Data/Images/Makefile
galaxium
galaxium.desktop
])

echo ""
echo "Configuration summary"
echo ""
echo "   * Installation prefix: $prefix"
echo ""
echo "   * Supported protocols"
echo "   *     ($enable_msn) MSN (Microsoft Network)"
echo ""
echo "   * HTML Layout Engines"
echo "   *     ($enable_gecko) Gecko support"
echo "   *     ($enable_webkit) WebKit support"
echo ""
echo "   * Additional addins"
echo "   *     ($enable_adium) AdiumX theme support"
echo ""
